package com.revzone.demo001.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.revzone.demo001.entity.PageResultMap;
import com.revzone.demo001.entity.Upload;
import com.revzone.demo001.entity.UploadGroup;
import com.revzone.demo001.service.UploadService;
import com.revzone.demo001.mapper.UploadMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
* @author Admin
* @description 针对表【upload】的数据库操作Service实现
* @createDate 2025-07-01 16:05:29
*/
@Service
public class UploadServiceImpl extends ServiceImpl<UploadMapper, Upload>
    implements UploadService{
    @Autowired
    private UploadMapper uploadGroupMapper;

    public Page<UploadGroup> groupByProIdPage(PageResultMap<UploadGroup> pageResultMap) {

        String pro_id = null;
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        if(pageResultMap.map !=null){
            UploadGroup dataMap =  pageResultMap.map;
            pro_id = dataMap.getPro_id();
            startTime = dataMap.getStartTime();
            endTime = dataMap.getEndTime();
        }

//        // 提取查询参数
        String proId = null;
//
//        // 设置默认分页参数
        Long current = (pageResultMap != null) ? pageResultMap.getCurrentPage() : 1;
        Long pageSize = (pageResultMap != null) ? pageResultMap.getPageSize() : 10;
//
//        // 计算分页偏移量
        long offset = (long) (current - 1) * pageSize;
        List<UploadGroup> records;
        Long total;
        if(pro_id != null){
            // 查询分组数据
            records = uploadGroupMapper.groupByProIdPageSearch(pageSize, offset, pro_id);

            // 查询分组总数
            total = (long) records.size();
        }else if(startTime != null && endTime!= null){
            // 查询分组数据
            records = uploadGroupMapper.groupByProIdPageSearchTime(pageSize, offset, startTime, endTime);

            // 查询分组总数
            total = (long) records.size();
        }
        else {
            // 查询分组数据
            records = uploadGroupMapper.groupByProIdPage(pageSize, offset);

            // 查询分组总数
            total = uploadGroupMapper.countGroups(proId);
        }

        // 创建分页对象
        Page page = new Page<>(pageResultMap.getCurrentPage(),pageResultMap.getPageSize());
        page.setRecords(records);
        page.setTotal(total);

        return page;
    }


}




